package com.xls.box2023.jinbi;

import androidx.recyclerview.widget.RecyclerView;

import com.lzz.bottomencapsulation.consts.Consts;
import com.lzz.bottomencapsulation.net.RequestCallBack;
import com.xls.box2023.adapter.YouXiXiaoHaoJinBiListAdapter;
import com.xls.box2023.entity.AccountBalanceEntity;
import com.xls.box2023.entity.PageEntity;
import com.xls.box2023.entity.YouXiXiaoHaoJinBiEntity;
import com.xls.box2023.home.HomeNetWorkUtil;
import com.xls.box2023.mvp.BasePresenterImpl;
import com.xls.xrecyclerview.XRecyclerView;

import java.util.ArrayList;

/**
 * MVPPlugin
 */

public class JinBiPresenter extends BasePresenterImpl<JinBiContract.View> implements JinBiContract.Presenter{
    YouXiXiaoHaoJinBiListAdapter youXiXiaoHaoJinBiListAdapter;
    ArrayList<YouXiXiaoHaoJinBiEntity> youXiXiaoHaoJinBiDataList=new ArrayList<>();
    int pageSize= Consts.PageSize,pageNum=Consts.PageNum;
    @Override
    public RecyclerView.Adapter getJinBiListAdapter() {
        if (youXiXiaoHaoJinBiListAdapter==null){
            youXiXiaoHaoJinBiListAdapter=new YouXiXiaoHaoJinBiListAdapter(context,youXiXiaoHaoJinBiDataList);
        }
        return youXiXiaoHaoJinBiListAdapter;
    }

    @Override
    public XRecyclerView.LoadingListener getLoadingListener() {
        return new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                pageNum=Consts.PageNum;
                getSourceData();
            }

            @Override
            public void onLoadMore() {
                pageNum=pageNum+1;
            }
        };
    }

    @Override
    public void getSourceData() {
        super.getSourceData();
        HomeNetWorkUtil.getInstance().getAccountBalance(new RequestCallBack<AccountBalanceEntity>() {
            @Override
            public void onStart() {

            }

            @Override
            public void onStop() {

            }

            @Override
            public void onRequestSuccess(AccountBalanceEntity requestData) {
                mView.showJinBiNum(requestData.getCoins());
            }

            @Override
            public void onRequestfail(String errno, String errmsg) {

            }
        });
        JinBiNetWorkUtil.getInstance().getMyDownLoadAppRecords(pageSize, pageNum, new RequestCallBack<PageEntity<YouXiXiaoHaoJinBiEntity>>(){
            @Override
            public void onStart() {

            }

            @Override
            public void onStop() {
                if (mView==null||context==null)return;
                mView.getListView().refreshComplete();
                mView.getListView().loadMoreComplete();
            }

            @Override
            public void onRequestSuccess(PageEntity<YouXiXiaoHaoJinBiEntity> requestData) {
                if (mView==null||context==null||requestData==null)return;
                if (pageNum==Consts.PageNum){
                    youXiXiaoHaoJinBiDataList.clear();
                }
                if (requestData.getRecords()!=null) {
                    youXiXiaoHaoJinBiDataList.addAll(requestData.getRecords());
                }
                if (youXiXiaoHaoJinBiDataList.size()>=requestData.getTotal()){
                    mView.getListView().setNoMore(true);
                }else {
                    mView.getListView().setNoMore(false);
                }
                youXiXiaoHaoJinBiListAdapter.notifyDataSetChanged();
            }

            @Override
            public void onRequestfail(String errno, String errmsg) {

            }
        });
    }
}
